//
//  AffirmOrderViewController.m
//  much
//
//  Created by weijunjie on 2019/1/10.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "AffirmOrderViewController.h"
#import "AffirmOrderHeaderView.h"
#import "AffirmOrderFooterView.h"
#import "AffirmOrderHeaderCell.h"
#import "AffirmOrderCell.h"
#import "OrderDataModel.h"
#import "GoodsModel.h"

@interface AffirmOrderViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) AffirmOrderHeaderView * affirmOrderHeaderView; // 收货地址
@property (strong, nonatomic) AffirmOrderFooterView * affirmOrderFooterView; // 底部价格与运费

@end

@implementation AffirmOrderViewController

- (void)_reLayout {
    self.navigationView.title = @"确认订单";
    self.tableView.tableHeaderView = self.affirmOrderHeaderView;
    self.tableView.tableFooterView = self.affirmOrderFooterView;
    [self.tableView reloadData];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        StoreAddressModel *model = [[StoreAddressModel alloc] init];
        model.name = @"张三";
        model.phone = @"18214578963";
        model.areaName = @"北京市海淀区";
        model.address = @"宏济中路88号sohoB座413";
        self.affirmOrderHeaderView.model = model;
        
        [self.affirmOrderFooterView setOrderWithTotalAmount:@"99" totalFreight:@"0"];
    });
    
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        CGRect frame = self.view.bounds;
        frame.size.height -= 49;
        _tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color244;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataArray = [[NSMutableArray alloc] init];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view);
            make.top.equalTo(self.navigationView.mas_bottom);
            make.bottom.equalTo(self.view).with.offset(-50);
            make.right.equalTo(self.view);
        }];
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 40, 0);
    }
    return _tableView;
}

- (AffirmOrderHeaderView *)affirmOrderHeaderView {
    if (!_affirmOrderHeaderView) {
        _affirmOrderHeaderView = [[AffirmOrderHeaderView alloc] init];
        _affirmOrderHeaderView.model = nil;
        [_affirmOrderHeaderView tapUpWithBlock:^(UIView * v) {
            
        }];
    }
    return _affirmOrderHeaderView;
}

- (AffirmOrderFooterView *)affirmOrderFooterView {
    if (!_affirmOrderFooterView) {
        _affirmOrderFooterView = [[AffirmOrderFooterView alloc] init];
    }
    return _affirmOrderFooterView;
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
//    return self.orderDataModel.orders.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
//    Orders *order = self.orderDataModel.orders[section];
//    return order.spus.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AffirmOrderCell *cell = [AffirmOrderCell orderCellWithTableView:tableView];
//    Orders *order = self.orderDataModel.orders[indexPath.section];
//    cell.model = order.spus[indexPath.row];
    GoodsModel *model = [[GoodsModel alloc] init];
    
    model.imagePath = @"https://img.yuntujk.com/31/20190103/4534355.jpg-w220";
    model.title = @"耍猫 酰醒葛根解酒茶醒酒茶 30包";
    model.name = @"规格:30包/袋";
    model.sellingPrice = @"176";
    model.marketSellingPrice = @"335";
    model.number = 2;
    model.minimum = 1;
    model.inventoryCount = 10;
    cell.model = model;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    AffirmOrderHeaderCell *headerCell = [AffirmOrderHeaderCell orderHeaderCellWithTableView:tableView];
    headerCell.storeName = @"竹叶青官方直供店";
    return headerCell;
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    ((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = [UIColor whiteColor];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 140;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

@end
